package net.pm.core.web.product;

import java.util.List;

import javax.annotation.Resource;

import net.pm.core.annotation.PowerCode;
import net.pm.core.annotation.PowerCode.PowerCodeEnum;
import net.pm.core.entities.db.Orderinfo;
import net.pm.core.service.MateriallogServicer;
import net.pm.core.service.OrderServicer;
import net.pm.core.service.ProductServicer;
import net.pm.core.web.BaseController;
import net.pm.core.web.order.OrderController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Scope("request")
@RequestMapping("/produce/")
public class ProduceController extends BaseController{
	
	@Resource(name="OrderServicer")
	private OrderServicer orderServicer;
	@Resource(name="ProductServicer")
	private ProductServicer productServicer;
	
	/**
	 * 去到生产管理页面
	 * @return
	 */
	@RequestMapping(value="manage",method=RequestMethod.GET)
	public String manage(){
		return "produce/produce_manage";
	}
	
	/**
	 * 自动转到默认列表
	 * 
	 * @return
	 * */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView list() {
		return list(null);
	}

	/**
	 * 搜索相关的生产中的订单
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public ModelAndView list(Long userId) {
		ModelAndView mav = new ModelAndView("produce/produce_list");
		Integer deptId = super.obtainCurrentDept().getId();
		List<Orderinfo> orders = orderServicer.findOrdersOfStatusProduce(deptId, userId);
		mav.addObject("orders", orders);
		mav.addObject("OrderController", super.useStaticPacker(OrderController.class.getName()));
		mav.addObject("Orderinfo", super.useStaticPacker(Orderinfo.class.getName()));
		return mav;
	}
	
	
	/**
	 * 订单产品生产完成,去填写一些相关信息
	 * @param productId
	 * @return
	 */
	@RequestMapping(value="finishproduce",method=RequestMethod.GET)
	public ModelAndView finishProduce(Long orderId){
		return new ModelAndView("/produce/produce_finish").addObject("orderId", orderId);
	}
	/**
	 * 订单产品生产完成
	 * @param orderId				订单id
	 * @param productNumber			生产数量
	 * @param remark				备注信息
	 * @return
	 */
	@RequestMapping(value="finishproduce",method=RequestMethod.POST)
	public String finishProduce(Long orderId,Integer productNumber,String remark){
		Long userId = super.obtainCurrentUser().getId();
		productServicer.finishProduce(orderId, productNumber, remark, userId);
		super.setSessionMessage("订单生产完成!");
		return "redirect:/produce/manage.htm";
	}
	
	
	/**
	 * 检测入库
	 * @param productId
	 * @return
	 */
	@PowerCode(code=PowerCodeEnum.CHECKIN_PRODUCT)
	@RequestMapping(value="checkinlibrary",method=RequestMethod.PUT)
	public String checkInLibrary(Long productId){
		//当前的用户id
		Long userId = super.obtainCurrentUser().getId();
		productServicer.checkInLibrary(productId, userId);
		super.setSessionMessage("检测入库操作成功!");
		return "redirect:/produce/manage.htm";
	}
	
}
